﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CONCURSO.EN;
using MJM.CBD.Conn;

namespace MJM.CONCURSO.AD
{
    public static class ResConcursanteAD
    {
        public static int Insertar(ResConcursanteEN obj)
        {
            int codigo = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_WEB_RESPAR_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vPAD_P_chIDPER", obj.IDPersona));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_P_inID", obj.IdConcurso));
                cmd.Parameters.Add(new SqlParameter("@vPRECON_P_inID", obj.IdPregunta));
                cmd.Parameters.Add(new SqlParameter("@vRESCON_P_inID", obj.IdRespuesta));
                cmd.Parameters.Add(new SqlParameter("@vRESPAR_inRESCOR", obj.EsCorrecta));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                codigo = 0;
            }
            return codigo;
        }

        public static List<ResConcursanteEN> Listar(ResConcursanteEN obj)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_RESPAR_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            List<ResConcursanteEN> lst = new List<ResConcursanteEN>();
            SqlDataReader dr;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vPAD_P_chIDPER", obj.IDPersona));
                cmd.Parameters.Add(new SqlParameter("@vCONVEC_P_inID", obj.IdConcurso));
                cmd.Parameters.Add(new SqlParameter("@vPRECON_P_inID", obj.IdPregunta));
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var objRes = new ResConcursanteEN();
                    objRes.EsCorrecta = dr.GetInt32(dr.GetOrdinal("RESPAR_inRESCOR"));
                    objRes.IDPersona = dr.GetString(dr.GetOrdinal("PAD_P_chIDPER"));
                    objRes.IdConcurso = dr.GetInt32(dr.GetOrdinal("CONVEC_P_inID"));
                    objRes.IdPregunta = dr.GetInt32(dr.GetOrdinal("PRECON_P_inID"));
                    objRes.IdRespuesta = dr.GetInt32(dr.GetOrdinal("RESCON_P_inID"));
                    lst.Add(objRes);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst;
        }
    }
}
